﻿using System;
using Besture.Bll;
using Besture.Common;
using Besture.Common.Helper;
using System.Web;
using Besture.Common.Definition;



namespace Besture.UI.Admin.Common
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
               var rememberPwd = StringHelper.Query("rememberPwd");
                var loginName = StringHelper.Query("loginName");
                var password = StringHelper.GetMD5(StringHelper.Query("password"));
                AdminUserDac adminUserDac = new AdminUserDac();
                //用户登录
                var loginFlag = adminUserDac.Login(loginName, password);
                if (loginFlag)
                {
                    //登录Cookie
                    HttpCookie userNameCookie = new HttpCookie("CtrlCRMLoginUserName", loginName);
                    userNameCookie.Domain = AppSettings.GetMainDomain();
                    userNameCookie.Path = "/";
                    Response.Cookies.Add(userNameCookie);

                    HttpCookie passwordCookie = new HttpCookie("CtrlCRMPassword", password);
                    passwordCookie.Domain = AppSettings.GetMainDomain();
                    passwordCookie.Path = "/";
                    Response.Cookies.Add(passwordCookie);

                    //记住用户名
                    if (Convert.ToBoolean(rememberPwd))
                    {
                        CookieHelper.SetCookie(Constants.COOKIE_ADMIN_USER_LOGINNAME, loginName, DateTime.Now.AddDays(7));
                    }
                    else
                    {
                        CookieHelper.RemoveCookie(Constants.COOKIE_ADMIN_USER_LOGINNAME);
                    }
                    Response.Write("0");

                }
                else
                {
                    Response.Write("1");
                }
                
            }
            catch (Exception exception)
            {
                Response.Write("1");
            }
        }

    }
}